<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\module\controllers;

use app\models\AmountRecorder;
use app\models\CateRecorder;
use app\models\DiscountRecorder;
use app\models\GoodsRecorder;
use app\models\NewRecorder;
use app\models\PurchaseRecorder;

class CateController extends BaseController{

    //获取顶级分类
    public function actionNav(){
        $list = CateRecorder::find()->select(['id','cate_name','thumb_pic'])->where('parent_id=0')->orderBy('cate_order DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //获取下级分类
    public function actionChildnav(){
        $id = \Yii::$app->request->post('id');
        $list = CateRecorder::find()->select(['id','cate_name','thumb_pic'])->where('parent_id=:id',[':id' => $id])->orderBy('cate_order DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //获取活动商品分类
    //$cate-顶级分类
    public function actionHdnav(){
        $cate = \Yii::$app->request->post('cate');
        $cate = intval($cate);
        $cate_model = CateRecorder::find()->select(['id'])->where('parent_id=:cate',[':cate' => $cate])->asArray()->all();
        $cate_arr=[];
        foreach($cate_model as $v){
            $cate_arr[]=$v['id'];
        }
        $arr=[];
        //新人购
        $model = NewRecorder::find()->select(['id','title','reduce'])->where('status=0')->asArray()->all();
        foreach($model as $v){
            $is = GoodsRecorder::find()->where('new_id='.$v['id'])->andWhere(['in','cate_id',$cate_arr])->count();
            if($is){
                $v['type']="new";
                $arr[]=$v;
            }
        }
        //限时购
        $model = PurchaseRecorder::find()->select(['id','title','reduce'])->where('status=0')->asArray()->all();
        foreach($model as $v){
            $is = GoodsRecorder::find()->where('purchase_id='.$v['id'])->andWhere(['in','cate_id',$cate_arr])->count();
            if($is){
                $v['type']="pur";
                $arr[]=$v;
            }
        }
        //满件减
        $model = DiscountRecorder::find()->select(['id','title'])->where('status=0')->asArray()->all();
        foreach($model as $v){
            $is = GoodsRecorder::find()->where('discount_id='.$v['id'])->andWhere(['in','cate_id',$cate_arr])->count();
            if($is){
                $v['type']="dis";
                $v['reduce']=0;
                $arr[]=$v;
            }
        }
        $list = CateRecorder::find()->select(['id','cate_name','thumb_pic'])->where('parent_id=:id',[':id' => $cate])->orderBy('cate_order DESC')->asArray()->all();
        foreach($list as $v){
            $v['type'] = "cate";
            $v['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
            $arr[] = $v;
        }
        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result);
    }

    //获取分类商品
    //参数：$cate-分类ID，$page-分页,$hd_id-活动ID，$type-活动类型
    public function actionGoods(){
        $post = \Yii::$app->request->post('page');
        $cate = intval($post['cate']);
        $page = intval($post['page'])-1;
        $hd_id = intval($post['hd_id']);//活动id
        $type = $post['hd_type'];//活动类型
        $pagesize = \Yii::$app->params['pagesize'];
        $offset = $page*$pagesize;
        //获取下级分类
        $child = CateRecorder::find()->select(['id']);
        if($cate){
            $child = $child->andWhere('parent_id=:cate',[':cate' => $cate]);
        }
        $child = $child->asArray()->all();
        $arr[]=$cate;
        if(!empty($child)) {
            foreach ($child as $v) {
                $arr[] = $v['id'];
            }
        }
        //获取内容
        $list = GoodsRecorder::find()->select(['id','goods_name','thumb_pic','goods_small_name','goods_num','goods_unit','sell_num',
            'goods_price','goods_hd_price','discount_id','new_id','purchase_id','amount_id']);
        switch($type){
            case "new":
                $list = $list->andWhere('new_id=:hd_id',[':hd_id' => $hd_id]);
                break;
            case "pur":
                $list = $list->andWhere('purchase_id=:hd_id',[':hd_id' => $hd_id]);
                break;
            case "dis":
                $list = $list->andWhere("discount_id=:hd_id",[':hd_id' => $hd_id]);
                break;
        }
        $list=$list->andWhere(['in','cate_id',$arr]);
        $list = $list->andWhere('is_down=0')->limit($pagesize)->offset($offset)->orderBy('addtime DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['goods_hd_price'] = empty($v['goods_hd_price'])?"":$v['goods_hd_price'];
            $list[$k]['percent'] = $this->get_percent($v['goods_num'],$v['sell_num']);
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
            $list[$k]['j_price'] = "";
            if($v['discount_id']){
                $model = DiscountRecorder::find()->where('id='.$v['discount_id'])->andWhere('status=0')->one();
                if($model){
                    $d = json_decode($model->content,true);
                    $list[$k]['discount_id'] = "满".$d[0]['count']."件打".$d[0]['reduce']."折";
                    $list[$k]['j_price'] = sprintf("%.2f",($v['goods_price']*((10-$d[0]['reduce'])/10)));
                }
            }else{
                $list[$k]['discount_id']='';
            }
            if($v['new_id']){
                $model = NewRecorder::find()->where('id='.$v['new_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['new_id'] = $model->title;
                    $list[$k]['j_price'] = sprintf("%.2f",($v['goods_price']*((10-$model->reduce)/10)));
                }
            }else{
                $list[$k]['new_id']='';
            }
            if($v['purchase_id']){
                $model = PurchaseRecorder::find()->where('id='.$v['purchase_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['purchase_id'] = $model->title;
                    $list[$k]['j_price'] = sprintf("%.2f",($v['goods_price']*((10-$model->reduce)/10)));
                }
            }else{
                $list[$k]['purchase_id']='';
            }
            if($v['amount_id']){
                $model = AmountRecorder::find()->where('id='.$v['amount_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['amount_id'] = $model->title;
                }
            }else{
                $list[$k]['amount_id']='';
            }
        }
        if(empty($list)){
            $result = ['result'=>0,'data'=>'没有相关内容'];
        }else{
            $result = ['result'=>1,'data'=>$list];
        }

        return json_encode($result);
    }

    //获取栏目下热门商品
    //参数：$num-数量，$cate-分类id
    public function actionHotgoods(){
        $post = \Yii::$app->request->post('param');
        $num = intval($post['num']);
        $cate = intval($post['cate']);
        $num = empty($num)?\Yii::$app->params['pagesize']:$num;
        //获取下级分类
        $child = CateRecorder::find()->select(['id'])->andWhere('parent_id=:cate',[':cate' => $cate]);
        $child = $child->asArray()->all();
        $cate_arr[]=$cate;
        if(!empty($child)) {
            foreach ($child as $v) {
                $cate_arr[] = $v['id'];
            }
        }
        $list = GoodsRecorder::find()->select(['id','goods_name','goods_small_name','thumb_pic','goods_num','sell_num','goods_price'])->where(['in','cate_id',$cate_arr])
            ->andWhere('is_down=0')->orderBy('sell_num DESC')->limit($num)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }

        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

}
